package tn.bergaoui.openwebapp.wicket.pages;

import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.panel.EmptyPanel;

import tn.bergaoui.openwebapp.model.Player;
import tn.bergaoui.openwebapp.model.Question;
import tn.bergaoui.openwebapp.wicket.components.ISelectionListener;
import tn.bergaoui.openwebapp.wicket.components.ISubmitListener;

public class HomePage extends WebPage implements ISelectionListener,
		ISubmitListener {

	public HomePage() {
		add(HeaderContributor.forCss("/style.css"));


		setOutputMarkupId(true);

		add(new PlayerListPanel("playerListPanel", this, this));
		add(new QuestionListPanel("questionListPanel", this, this));
		add(new EmptyPanel("personPanel"));

	}

	@Override
	public void onSelectedItem(Long id, Class clazz) {
		if (clazz.equals(Player.class))
			addOrReplace(new PersonEditorPanel("personPanel", id, this, this));
		if (clazz.equals(Question.class))
			addOrReplace(new QuestionEditorPanel("personPanel", id, this, this));
	}

	private void refreshList() {
		addOrReplace(new PlayerListPanel("playerListPanel", this, this));
		addOrReplace(new QuestionListPanel("questionListPanel", this, this));
		addOrReplace(new EmptyPanel("personPanel"));
	}

	@Override
	public void onCancel() {
		addOrReplace(new EmptyPanel("personPanel"));
	}

	@Override
	public void onSubmit() {
		refreshList();
	}

}
